package com.brillilab.service.job.instrument;

import com.alibaba.fastjson.JSONObject;
import com.brillilab.domain.enums.instrument.AppointmentStateEnum;
import com.brillilab.domain.po.instrument.InstrumentAppointment;
import com.brillilab.service.core.instrument.IInstrumentAppointmentService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class AppointmentEnd implements Job {

    @Resource
    private IInstrumentAppointmentService instrumentAppointmentService;

    @Override
    public void execute(JobExecutionContext context) {
        log.info("预约状态已被设置为已结束——开始");

        JobDataMap map=context.getJobDetail().getJobDataMap();
        String appStr=map.getString("appointment");
        InstrumentAppointment appointment=JSONObject.parseObject(appStr,InstrumentAppointment.class);

        InstrumentAppointment exist=instrumentAppointmentService.selectById(appointment.getId());
        if(exist != null && exist.getState().equals(AppointmentStateEnum.START.getValue())){
            exist.setState(AppointmentStateEnum.END.getValue());
            instrumentAppointmentService.updateById(exist);
            log.info("预约状态已被设置为已结束，预约ID为:" + appointment.getId());
        } else {
            log.info("预约为已取消状态:" + appointment.getId());
        }
    }
}
